home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / eckelt01.zip / 6 / SSTACK.CPP < prev    next >
C/C++ Source or Header  |  1995-02-23  |  3KB  |  86 lines

  1. // File from page 292 in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. //: SSTACK.CPP -- Enums inside classes
  34. #include <string.h>
  35. #include <iostream.h>
  36.  
  37. class StringStack {
  38.   enum { size = 100 };
  39.   const char* stack[size];
  40.   int index;
  41. public:
  42.    StringStack();
  43.    void push(const char* s);
  44.    const char* pop();
  45. };
  46.  
  47. StringStack::StringStack() : index(0) {
  48.   memset(stack, 0, size * sizeof(char*));
  49. }
  50.  
  51. void StringStack::push(const char* s) {
  52.   if(index < size)
  53.     stack[index++] = s;
  54. }
  55.  
  56. const char* StringStack::pop() {
  57.   if(index > 0) {
  58.     const char* rv = stack[--index];
  59.     stack[index] = 0;
  60.     return rv;
  61.   }
  62.   return 0;
  63. }
  64.  
  65. const char* iceCream[] = {
  66.   "pralines & cream",
  67.   "fudge ripple",
  68.   "jamocha almond fudge",
  69.   "wild mountain blackberry",
  70.   "raspberry sorbet",
  71.   "lemon swirl",
  72.   "rocky road",
  73.   "deep chocolate fudge"
  74. };
  75.  
  76. const ICsz = sizeof iceCream/sizeof *iceCream;
  77.  
  78. main() {
  79.   StringStack SS;
  80.   for(int i = 0; i < ICsz; i++)
  81.     SS.push(iceCream[i]);
  82.   const char* cp;
  83.   while((cp = SS.pop()) != 0)
  84.     cout << cp << endl;
  85. }
  86.